<?php

include 'util/pdo.php';

$pdo = new pdo_util();

switch ($_GET['source']) {

    // 搜索
    case "search":
        $userInfo = $pdo->select('user', 'name,phone', ['status' => 1, 'name' => $_GET['username']]);
        break;
    // 删除
    case "del":
        $pdo->update('user', ['status'=>0], ['id'=>$_GET['id']]);
        $userInfo = $pdo->select('user', 'id,name,phone', ['status' => 1]);
        break;
    default:

        if (!$_COOKIE['user'] || $_COOKIE['user']=='null') {
            header("location:/sw/html/login.html");
        }

        $userInfo = $pdo->select('user', 'id,name,phone', ['status' => 1]);

}

?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>列表</title>

    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.min.js"></script>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>
<div class="box">
    <div class="content">

        <!--搜索-->
        <div class="container content_width">
            <div class="person_search">
                <form action="list.php" method="get">
                    <div class="search_input">
                        <div class="">
                            <input name="username" id="username" type="text" class="form-control" placeholder="请输入姓名">
                            <input name="source" type="hidden" value="search">
                        </div>
                    </div>
                    <div class="search_input">
                        <button type="submit" class="btn btn-primary search_btn" type="button">查询</button>
                    </div>
                </form>

                <div class="search_input">
                    <button onclick="document.location = 'list.php'" class="btn btn-primary back" type="button">返回</button>
                </div>
                <div class="search_input">
                    <button id="logout" class="btn btn-primary logout" type="button">退出登录</button>
                </div>
            </div>
            <div class="line"></div>
        </div>

        <!--表格-->
        <table id="tb" class="table">
            <thead>
            <tr>
                <th>姓名</th>
                <th>手机号</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="show_tbody">
            <?php if(!empty($userInfo)) { ?>
            <?php foreach ($userInfo as $k => $v) { ?>
            <tr>
                <td><?= $v['name'] ?></td>
                <td><?= $v['phone'] ?></td>
                <td>
                    <a href="php/edit.php?source=edit&id=<?= $v['id'] ?>&username=<?= $v['name'] ?>&phone=<?= $v['phone'] ?>" class="edit">编辑</a>
                    <a href="list.php?source=del&id=<?= $v['id'] ?>" class="del">删除</a>
                </td>
            </tr>
            <?php  } ?>
            <?php } ?>

            </tbody>
        </table>

    </div>
</div>
</body>
</html>

<script>
    $("#logout").click(function(event){
        $.cookie('user', null, {path:'/'});
        document.location = 'list.php'
    })
</script>
